var searchEnergyDetail;
//跳转到线路信息页面
var goLineInfo;
require(['grid', 'topBar', 'layout', 'date'], function () {
    var postData = null;
    	postData = {};
    var $gridList = $('#grid'),
        $top = $('#top');
    $('body').layout();
    $top.topBar({
        left: [{
            type: 'filter-more',
            dateType: 'custom',
            search: function () {
                $gridList.grid('setGridParam', {
                    url: $.getProjectName() + "/deptEnergy/searchDeptEnergy.htm"
                });
                if (getPostData()) {
                    $gridList.grid('setGridParam', {
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    type: 'dateLine'
                }, {
                    text: '按条件汇总',
                    name: 'energyTimeType',
                    type: 'item-select',
                    data: [
                        {text: '按日汇总', value: '10'},
                        {text: '按月汇总', value: '7'},
                        {text: '总体汇总', value: ''}
                    ]
                }, {
                    text: '燃料类型',
                    name: 'fuelTypeCode',
                    type: 'item-select',
                    data: [
                        {text: '全部', value: ''},
                        {text: '柴油', value: '1'},
                        {text: '电能', value: '3'},
                        {text: 'CNG', value: '4'},
                        {text: 'LNG', value: '5'},
                        {text: '汽油', value: '2'}
                    ]
                }
            ]
        }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });

    function exportFile(suffix) {
        if ($gridList.getGridParam("reccount") > 0 && getPostData()) {
            //var $postData=$("#top").parent().getValues();
            var url = "/deptEnergy/exportDeptEnergy" + suffix + ".htm";
            $.exportFile(url, postData);
        } else {
            $.messager.alert("警告", "请先查询数据！", "info");
        }
    }

    $gridList.grid({
        url: "/deptEnergy/searchDeptEnergy.htm",
        colModel: [
            {name: "id", key: true, hidden: true, label: "id"},
            {name: "lineId",hidden: true, label: "lineId"},
            {name: "fuelTypeCode", key: true, hidden: true, label: "fuelTypeCode"},
            {
                name: "lineName", sorttype: "text", align: "left", label: "营运线路", width:80,formatter: 'customCurrency',formatoptions:{
	                redbtn:true,
	                css:'text-link c-blue',
	                clickFn:function(rowObject,cellvalue){
	                	goLineInfo(rowObject.lineId);
	                }
	            }
            },
            {
                name: "organName", sorttype: "text", align: "left", label: "所属机构", width:80,
                cellattr: function (rowId) {
                    return 'id=\'organName' + rowId + "\'";
                }
            },
            {
                name: "days", align: "center", label: "运营日期", width:80,
                cellattr: function (rowId) {
                    return 'id=\'days' + rowId + "\'";
                }
            },
            {name: "rngOprtDuraTr", align: "right", width:80,label: "运行时长"},
            {name: "rngMile", align: "right", formatter: "currency", width:80,label: "运行里程"},
            {name: "fuelTypeName", align: "center", width: 80, width:80,label: "燃料类型"},
            {name: "rngEnerCons", align: "right", formatter: "currency", width:80,label: "运行能耗"},
            {name: "enerConsMile", align: "right", formatter: "currency", width:80,label: "百公里能耗"},
//            {name: "refrEnerCons", align: "right", formatter: "currency", width:80,label: "标准能耗"},
//            {name: "enerConsPers", align: "right", width:80,label: "能耗百分比"},
//            {name: "lineEnerConsMile", align: "right", formatter: "currency", width:80,label: "线路平均能耗"},
            {name: "isSaveEner", align: "left", width:80,label: "是否节能"}
        ],
        multiselect: false,
        gridComplete: function () {
//            Merger('lineName');
            Merger('organName');
            Merger('days');
        }
    });
    
  //跳转到线路信息页面
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }

    //获取需要传递到后台的参数
    function getPostData() {
        var type = 'LINE';
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (orgIds == null || !orgIds.length) {
            $.messager.alert("提示", "请选择要查询的线路!", "warning");
            return false;
        }
        postData = $top.parent().getValues();
        postData.orgIds = orgIds.toString();
        postData.type = type;
        postData.energyType = "";
        return true;
    }

    searchEnergyDetail = function (data) {
        //TODO

        var node = commonFun.getNodesByType("LINE");
        if (node && node.length > 0) {
            commonFun.setCheckedNodes(data.lineId);
            $("a[data-val]", $top.parent()).removeClass("active");
            $("a[data-val='" + data.energyTimeType + "']", $top.parent()).addClass("active");
            $("a[data-val='" + data.dateType + "']", $top.parent()).addClass("active");
            $gridList.grid('setGridParam', {
                url: $.getProjectName() + "/deptEnergy/getEnergyDetail.htm",
                datatype: "json",
                postData: {
                    "lineId": data.lineId,
                    "energyType": data.energyType,
                    "energyTimeType": data.energyTimeType,
                    "dateType": data.dateType
                }
            }).trigger("reloadGrid");
        }


    };

    function Merger(CellName) {
        //得到显示到界面的id集合
        var mya = $gridList.getDataIDs();
        //当前显示多少条
        var length = mya.length;
        for (var i = 0; i < length; i++) {
            //从上到下获取一条信息
            var before = $gridList.jqGrid('getRowData', mya[i]);
            //定义合并行数
            var rowSpanTaxCount = 1;
            for (var j = i + 1; j <= length; j++) {
                //和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏
                var end = $gridList.jqGrid('getRowData', mya[j]);

                if (before[CellName] == end[CellName] && before['days'] == end['days']) {
                    rowSpanTaxCount++;
                    $gridList.setCell(mya[j], CellName, '', {display: 'none'});
                } else {
                    rowSpanTaxCount = 1;
                    break;
                }
                $("#" + CellName + "" + mya[i] + "").attr("rowspan", rowSpanTaxCount);
            }
        }
    }
});
